package com.ithema.manger.dao;

import com.ithema.manger.domain.Student;

public class StudentDao {
    //创建Student类的数组
    private static Student[] student = new Student[5];
    //添加学生信息
    public boolean addStudent(Student stu) {

        //遍历数组,并添加信息
        for (int i = 0; i < student.length; i++) {
            Student students = student[i];
            if (students == null) {
                student[i] = stu;
                return true;
            }
        }
        return false;
    }
//查看学生信息
    public Student[] findAllStudent() {
        return student;
    }
//删除学生
    public void delectStudent(String sid) {
        //调用方法查找学号对应索引
        int index = getIndex(sid);
        //使对应索引值为null
        student[index]=null;
    }
    //定义方法,根据学号查找索引
    public int getIndex(String sid){
        //初始值
        int index=-1;
        for (int i = 0; i < student.length; i++) {
            Student stu=student[i];
            if(stu!=null&&sid.equals(stu.getSid())){
                index=i;
            }
        }return index;
    }
//修改学生信息
    public void updateStudent(String updateSid, Student updateStu) {
        //调用getIndex方法对新的学号进行查找索引
        int index = getIndex(updateSid);
        //修改信息
        student[index]=updateStu;
    }
}
